<%--
  Created by IntelliJ IDEA.
  User: dell
  Date: 2024/8/6
  Time: 11:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>addStudent</title>
    <link href="static/layui/css/layui.css" rel="stylesheet">
</head>
<body>
    <script src="static/layui/layui.js" type="text/javascript" charset="UTF-8"></script>
    <script src="static/jquery-2.1.4.js" type="text/javascript" charset="UTF-8"></script>
    <script src="static/mylayer.js" type="text/javascript" charset="UTF-8"></script>

    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name" autocomplete="off" placeholder="请输入姓名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">年龄</label>
            <div class="layui-input-block">
                <input type="text" name="age" autocomplete="off" placeholder="请输入年龄" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <input type="text" name="gender" autocomplete="off" placeholder="请输入性别" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">班级</label>
            <div class="layui-input-block">
                <select id="banjiId" name="banjiId">
                    <option>请选择班级</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="submitDemo">添加</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

    <script>



        layui.use(['form'], function () {
            var form = layui.form;

            //监听提交
            form.on('submit(submitDemo)', function(data){
                // layer.alert(JSON.stringify(data.field), {
                //     title: '最终的提交信息'
                // })
                console.log(data.field);
                $.post(
                    '/student?method=add',
                    data.field,
                    function (result) {
                        console.log(result);
                        if(result.code == 0) {
                            mylayer.okMsg(result.msg);
                            setInterval(function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                window.parent.location.reload();
                            }, 2000);
                        }
                    },
                    'json'
                )
                return false;
            });

            form.render(
                $(function () {
                    $.post(
                        '/banji?method=selectAll',
                        {'banjiId':$(this).val()},
                        function (jsonObj) {
                            console.log(jsonObj);
                            $(jsonObj).each(function () {
                                $('#banjiId').append('<option value="' + this.id + '">' + this.name + '</option>');
                            })
                            layui.use('form', function () {
                                var form = layui.form;
                                form.render();
                            });
                        },
                        'json'
                    )
                })
            );

        })

    </script>
</body>
</html>
